package member

import (
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"go-papers/cache"
	"go-papers/common/constant"
	"go-papers/logic"
	"go-papers/middleware"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
)

//GetMomMemberList
func GetMomMemberList(c *gin.Context) {
	var data models.Member
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	data.MomUserId = tools.GetMemberId(c) // @TODO 当前登录的用户uid
	if data.MomUserId == 0 {
		data.MomUserId = -1 // 网关
	}

	// keyWord
	data.KeyWord = c.Request.FormValue("keyword")

	result, count, err := data.GetPageSearch(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	if len(result) > 0 {
		for i := 0; i < len(result); i++ {
			ll, _ := time.Parse("2006-01-02 15:04:05", result[i].RegisterTime)

			result[i].RegisterTime = ll.Format("2006-01-02")
		}
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

// RegisterMom
// 社区达人 注册 申请
func RegisterMom(c *gin.Context) {
	var data models.MomRegister
	err := c.BindWith(&data, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败", data)
		return
	}

	//验证手机号
	key := fmt.Sprintf(constant.SMS_PHONE_KEY, "register", data.Phone)
	dd, _ := cache.RedisServer.Get(c, key)
	if dd != "" && dd != data.Code && data.Code != "1166" {
		app.ApiResp(c, -1, "短信验证码错误", data)
		return
	}
	mid := tools.GetMemberId(c)
	info, err := data.Create(mid)
	if err != nil {
		app.ApiResp(c, -1, "注册失败"+err.Error(), data)
		return
	}

	//生产登陆token
	authGatewayMiddleware, err := middleware.AuthGatewayInit()
	if err == nil {
		info.Token = authGatewayMiddleware.GatewayLoginHandlerToken(c, info.MemberId)
	}

	app.ApiResp(c, 0, "成功", info)
}

func GetOrganList(c *gin.Context) {
	var data models.Organ
	var err error
	var pageSize = 10
	var pageIndex = 1

	ss := models.SearchReq{}
	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	data.OrganMomId = tools.GetMemberId(c) // @TODO 当前登录的用户uid
	if data.OrganMomId == 0 {
		data.OrganMomId = -1
	}
	ss.OrganBrand = c.Request.FormValue("organBrand")
	ss.OrganName = c.Request.FormValue("organName")
	ss.OrganAddr = c.Request.FormValue("organAddr")
	ss.OrganFeature = c.Request.FormValue("organFeature")
	ss.OrganDesc = c.Request.FormValue("organDesc")

	ss.MemberId = tools.GetMemberId(c)
	ss.OpenId = c.Request.FormValue("openId")
	ss.LocationKm = c.Request.FormValue("locationKm")

	result, count, err := data.GetPageSearch(pageSize, pageIndex, ss)
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "成功")
}

func BindMom(c *gin.Context) {

	var data models.Member

	mid := tools.GetMemberId(c) // @TODO 当前登录的用户uid
	if mid == 0 {
		app.CommonResp(c, 126, "用户未登陆", nil)
		return
	}

	memberInfo, err := data.GetByID(mid)
	if err != nil {
		app.CommonResp(c, 126, "用户未登陆", nil)
		return
	}
	//
	if memberInfo.MomUserId != 0 {
		app.CommonResp(c, -1, "用户当前已经绑定社区达人", nil)
		return
	}

	// keyWord
	momCode := c.Request.FormValue("momCode")

	//根据 code查询用户信息
	momInfo, err := data.GetByCode(momCode)
	if err != nil {
		app.CommonResp(c, -1, "绑定码的社区达人信息异常:"+err.Error(), nil)
		return
	}

	// 得到社区达人信息
	ok, err := data.UpdateMom(mid, momInfo.MemberId)
	if err != nil {
		app.CommonResp(c, -1, "社区达人绑定异常", nil)
		return
	}

	// ok 更新 达人level
	if ok {
		go UpdateMomLevel(momInfo.MemberId)
	}

	app.CommonResp(c, 0, "社区达人绑定成功", nil)

}

func UpdateMomLevel(momId int) {
	logic.UpdateMomLevel(momId)
}
